package com.android.Lmaps;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DBphongtro {
	private SQLiteDatabase database =null;
	private Activity context;
	public DBphongtro() {
		
	}
	public DBphongtro(Activity context) {
		this.setContext(context);
		
		DBConfig config =new DBConfig("phongtro", context);
		database = config.getdatabase();
		
	}
	public ArrayList<phongtro> findAll() {
		ArrayList<phongtro> list = new ArrayList<phongtro>();
		
		Cursor c=null;
		
		try {
			database.query("phongtro", null, null, null, null, null, null);
		
		} catch (Exception e) {
			// TODO: handle exception
		
			String sql ="CREATE TABLE phongtro (";
				sql+="id TEXT PRIMARY KEY,";
				sql+="latitude DOUBLE,";
				sql+="longitude DOUBLE,";
				sql+="address TEXT,";
				sql+="price DOUBLE,";
				sql+="description TEXT,";
				sql+="hour NUMBERIC,";
				sql+="minute NUMBERIC,";
				sql+="day NUMBERIC,";
				sql+="month NUMBERIC,";
				sql+="year NUMBERIC,";
				sql+="contact TEXT,";
				sql+="personshare NUMBERIC)";
				
				database.execSQL(sql);
				return list;
			
		}
		
		c = database.query("phongtro", null, null, null, null, null, null);
		
		c.moveToFirst();
		
		while (c.isAfterLast()==false)
		{
		
			phongtro pt = new phongtro();
			pt.setId(c.getString(0));
			pt.setLat(c.getDouble(1));
			pt.setLon(c.getDouble(2));
			pt.setAddress(c.getString(3));
			pt.setPrice(c.getDouble(4));
			pt.setDescription(c.getString(5));
			pt.setHour(c.getInt(6));
			pt.setMinute(c.getInt(7));
			pt.setDay(c.getInt(8));
			pt.setMonth(c.getInt(9));
			pt.setYear(c.getInt(10));
			pt.setContact(c.getString(11));
			int pss =c.getInt(12);
			if(pss==0)
			{
				pt.setPersonShare(false);
			}
			else
			{
				pt.setPersonShare(true);
			}
			
			list.add(pt);
		
			c.moveToNext();
		
		}
		c.close();
		
		return list;
	}
	public boolean createphongtro(phongtro pt) {
		
		ContentValues values =new ContentValues();
		
		values.put("id",pt.getId());
		values.put("latitude",pt.getLat());
		values.put("longitude",pt.getLon());
		values.put("address",pt.getAddress());
		values.put("price",pt.getPrice());
		values.put("description",pt.getDescription());
		values.put("hour",pt.getHour());
		values.put("minute",pt.getMinute());
		values.put("day",pt.getDay());
		values.put("month",pt.getMonth());
		values.put("year",pt.getYear());
		values.put("contact",pt.getContact());
		
		if(pt.getPersonShare())
		{
			values.put("personshare",1);
		}
		else
		{
			values.put("personshare",0);
		}
		
		
		if(database.insert("phongtro", null, values)==-1)
			return false;
		else
			return true;
	}
	
	public boolean removephongtro(String id) {
		id="id="+id;
		if(database.delete("phongtro",id , null)==-1)
			return false;
		else
			return true;
	}
	
	public boolean updatephongtro(phongtro pt) {
		ContentValues values =new ContentValues();
		
		values.put("id",pt.getId());
		values.put("latitude",pt.getLat());
		values.put("longitude",pt.getLon());
		values.put("address",pt.getAddress());
		values.put("price",pt.getPrice());
		values.put("description",pt.getDescription());
		values.put("hour",pt.getHour());
		values.put("minute",pt.getMinute());
		values.put("day",pt.getDay());
		values.put("month",pt.getMonth());
		values.put("year",pt.getYear());
		values.put("contact",pt.getContact());
		values.put("contact",pt.getContact());
		
		if(pt.getPersonShare())
		{
			values.put("personshare",1);
		}
		else
		{
			values.put("personshare",0);
		}
		
		if(database.update("phongtro",values, "id = "+pt.getId(), null)==-1)
			return false;
		else
			return true;
	}
	
	public Activity getContext() {
		return context;
	}
	public void setContext(Activity context) {
		this.context = context;
	}
	
}
